package com.jmgd.cjlx.controller;

import com.jmgd.cjlx.core.entity.ResultEntity;
import com.jmgd.cjlx.core.enums.ResultEnum;
import com.jmgd.cjlx.dao.JmgdMapper;
import com.jmgd.cjlx.service.JztService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/jzt")
public class JztController {
    private static final Logger logger = LoggerFactory.getLogger(JztController.class);

    @Autowired
    JztService jztService;

    @Autowired
    JmgdMapper jmgdMapper;


    @RequestMapping("/test")
    public ResultEntity test() throws Exception {
//        dzService.queryPriceBatch();
//        dzService.queryStockBatch();
//        jmgdMapper.updateWarePrice();
        logger.info("九州通每天拉取供应商库存和价格");

        jztService.getWareKcAndPrice();

        jmgdMapper.updateWarePrice();
        return new ResultEntity(ResultEnum.SUCCESS,null);
    }


    @RequestMapping("/addDrug")
    public ResultEntity addDrug() throws Exception {
        for(int i = 0;i<10;i++) {
            jztService.addDrug();
        }
        return new ResultEntity(ResultEnum.SUCCESS,null);
    }

    @RequestMapping("/getToken")
    public ResultEntity getToken() throws Exception {
        return new ResultEntity(ResultEnum.SUCCESS,jztService.getToken());
    }

    //getWareKcAndPrice
    @RequestMapping("/getWareKcAndPrice")
    public ResultEntity getWareKcAndPrice() throws Exception {
        jztService.getWareKcAndPrice();
        return new ResultEntity(ResultEnum.SUCCESS,null);
    }

    @RequestMapping("/insertCgxx")
    public ResultEntity insertCgxx(@RequestParam(value = "orderCode") String orderCode) throws Exception {
        jztService.insertCgxx("九州通",orderCode);
        return new ResultEntity(ResultEnum.SUCCESS,null);
    }

    @RequestMapping("/getTaXxYtq1")
    public ResultEntity getTaXxYtq1() throws Exception {
        jztService.getTaXxYtq1();
        return new ResultEntity(ResultEnum.SUCCESS,null);
    }

    @PostMapping("/callBack")
    public ResultEntity callBack(@RequestBody Object object) {
        Date now = new Date();

        // 定义日期格式，包括年、月、日、时、分、秒和毫秒
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        // 将当前时间格式化为字符串
        String formattedDate = sdf.format(now);

        logger.info("回调开始"+formattedDate);
        jztService.callBack(object);

        Date nowEnd = new Date();
        String formattedEndDate = sdf.format(nowEnd);
        logger.info("回调结束1"+formattedEndDate);
        return new ResultEntity(ResultEnum.SAVE_SUCCESS,null);
    }

}
